﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System;

namespace GX
{
	public static class Reflection
	{
		/// <summary>
		/// 获得每一个程序集的所有type(类)
		/// </summary>
		/// <param name="asm"></param>
		/// <returns></returns>
		public static IEnumerable<Type> GetAllTypes(this Assembly asm)
		{
			return asm.GetTypes();
		}

		/// <summary>
		/// 获得所有正在运行的程序集
		/// </summary>
		/// <returns></returns>
		public static Assembly GetExecutingAssembly()
		{
			return typeof(Reflection).Assembly;
		}

		/// <summary>
		/// 获得正在运行的方法
		/// </summary>
		/// <param name="type"></param>
		/// <returns></returns>
		public static IEnumerable<MethodInfo> GetRuntimeMethods(this Type type)
		{
			return type.GetMethods();
		}

		/// <summary>
		/// 扩展method方法创建一个委托方法
		/// </summary>
		/// <param name="method"></param>
		/// <param name="delegateType"></param>
		/// <param name="target"></param>
		/// <returns></returns>
		public static Delegate CreateDelegate(this MethodInfo method, Type delegateType, object target = null)
		{
			if (method == null)
				return null;
			return target == null ? Delegate.CreateDelegate(delegateType, method) : Delegate.CreateDelegate(delegateType, target, method);
		}

		/// <summary>
		/// 扩展委托的代理方法
		/// </summary>
		/// <param name="func"></param>
		/// <returns></returns>
		public static MethodInfo GetMethodInfo(this Delegate func)
		{
			return func.Method;
		}
	}
}
